package com.ds.api.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ds.api.dao.SheetInfoMapper;
import com.ds.api.dao.SysSheetMapper;
import com.ds.api.dao.UserBasicInfoMapper;
import com.ds.api.dao.UserKeyMapper;
import com.ds.api.model.SheetInfo;
import com.ds.api.model.SysSheet;
import com.ds.api.model.UserBasicInfo;
import com.ds.api.model.UserKey;
import com.ds.api.utils.FtpUploadUtil;
import com.google.gson.Gson;

@RestController
@RequestMapping("/sheet")
public class SheetController extends BaseController {
    @Autowired
    private SysSheetMapper sheetMapper;

    @Autowired
    private SheetInfoMapper mapper;
    
    @Autowired
    private UserKeyMapper keymapper;
    
    @Autowired
    private UserBasicInfoMapper infoMapper;
    private static final Logger log = LoggerFactory
            .getLogger(SheetController.class);
    /**
     * 用户下单
     * @param param
     * @param info
     * @param request
     * @param response
     */
    @RequestMapping(value = "/send")
    public  void send(@RequestParam Map<String,String> param,
            HttpServletRequest request, HttpServletResponse response) {
        String path="/opt/img/image/";
        String resultStr="0";
        Map<String, Object> result = new HashMap<String, Object>();
        String validity_date=param.get("validity_date");
        String workhours=param.get("workhours");
        String position_x=param.get("position_x");
        String position_y=param.get("position_y");
        String is_price=param.get("is_price");
        String send_price=param.get("send_price");
        String accpet_price=param.get("accpet_price");

        SysSheet sheet=new SysSheet();
        sheet.setValidityDate(validity_date);
        sheet.setWorkhours(workhours);
        sheet.setPositionX(position_x);
        sheet.setPositionY(position_y);
        sheet.setIsPrice(is_price);
        if(accpet_price!=null&&!"".equals(accpet_price)) {
            sheet.setAccpetPrice(Double.valueOf(accpet_price));
        }
        if(send_price!=null&&!"".equals(send_price)) {
            sheet.setSendPrice(Double.valueOf(send_price));
        }
        sheet.setType(param.get("type"));
        sheet.setFcd(new Date());
        sheet.setStatus("0");
        sheet.setRemark(param.get("remark"));
        sheet.setUserAddress(param.get("user_address"));
        sheet.setOrderTime(param.get("order_time"));
        sheet.setSendAddress(param.get("send_address"));
        if(param.get("fk_type_machine_id")!=null&&!"".equals(param.get("fk_type_machine_id"))){
            sheet.setFkTypeMachineId(Integer.parseInt(param.get("fk_type_machine_id")));
        }
        sheet.setPrice(Double.valueOf(param.get("price")));
        String user_id=request.getAttribute("USER_ID")+"";
        sheet.setFkSendUersBasicinfoUid(Integer.parseInt(user_id));
        sheet.setLmd(new Date());
        String pic="";
        for (int i = 1; i < 5; i++) {
            String pic1=param.get("img"+i);//照片一
            if(pic1!=null&&!"".equals(pic1)) {
                String imagepath=new Date().getTime()+".png";
                File file=FtpUploadUtil.GenerateImage(pic1,path+imagepath);
                if(file!=null) {
                    pic=pic+";"+imagepath;
                }
            } 
        }
        if(pic!=null) {
            sheet.setPic1(pic);
        }

        sheet.setQuantities(param.get("quantities"));
        result.put("result",resultStr);
        sheetMapper.insert(sheet);

        for (int i = 1; i < 6; i++) {
            String fk_machine_id=param.get("fk_machine_id"+i);
            String product_count=param.get("product_count"+i);
            if(fk_machine_id!=null&&!"".equals(fk_machine_id)&&!"undefined".equals(fk_machine_id)) {
                SheetInfo info=new SheetInfo();
                info.setFkMachineId(Integer.parseInt(fk_machine_id));
                if(product_count!=null&&!"".equals(product_count)&&!"undefined".equals(product_count)) {
                    info.setProductCount(Integer.parseInt(product_count));
                }
                info.setFkSysSheetId(sheet.getStid());
                mapper.insert(info);
            }
        }
        Gson gson = new Gson();
        try {
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));
        } catch (Exception e) {
            log.info("程序出错："+e);
        }

    }


//    @RequestMapping(value = "/query")
//    public  void query(@RequestParam Map<String,Object> param,
//            HttpServletRequest request, HttpServletResponse response) {
//
//        String resultStr="0";
//        Map<String, Object> result = new HashMap<String, Object>();
//        if(param.get("stid")!=null&&!"".equals(param.get("stid"))){
//            SysSheet sheet=sheetMapper.selectByPrimaryKey(Integer.parseInt(param.get("stid")+""));
//
//        }else{
//            String user_id=request.getAttribute("USER_ID")+"";
//            param.put("fkSendUersBasicinfoUid", user_id);
//            //            List<SysSheet> sys=
//        }
//    }


    @RequestMapping(value = "/list")
    public  void list(@RequestParam Map<String,Object> param,
            HttpServletRequest request, HttpServletResponse response) {
        String resultStr="0";
        Map<String, Object> result = new HashMap<String, Object>();
        Integer user_id=null;
        if(param.get("key")!=null&&!"".equals(param.get("key"))){
            UserKey exp = new UserKey();
            exp.setUidKey(param.get("key").toString());
            exp.setIsActive("0");
            List<UserKey> keyList = keymapper.query(exp);
            if (keyList.size() > 0) {
                user_id= keyList.get(0).getFkUserBaseinfoId();
            }
        }
        if(param.get("mineSend")!=null&&!"".equals(param.get("mineSend"))){
            param.put("fkSendUersBasicinfoUid", user_id);
        }
        if(param.get("mineOrder")!=null&&!"".equals(param.get("mineOrder"))){
            param.put("fkOrderUersBasicinfoUid", user_id);
        }
        String type=param.get("type")+"";
        if(type!=null&&!"".equals(type)) {
            List<String> lists=new ArrayList<String>();
            String []fkparentIds=type.split(",");
            for (String string : fkparentIds) {
                lists.add(string);
            }
            param.put("lists", lists);
        }
        List<SysSheet> list= sheetMapper.list(param);
        if(param.get("stid")!=null&&!"".equals(param.get("stid"))){
            result.put("sheetdetail",list.get(0));
        }else{
            result.put("list",list);
        }
        
        Gson gson = new Gson();
        try {
            result.put("result",resultStr);
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));
        } catch (Exception e) {
            log.info("程序出错："+e);
        }
    }
    
    /**
     * 接单
     * @param param
     * @param request
     * @param response
     */
    @RequestMapping(value = "/accept")
    public  void accept(@RequestParam Map<String,String> param,
            HttpServletRequest request, HttpServletResponse response) {

        String resultStr="0";
        Map<String, Object> result = new HashMap<String, Object>();
        String user_id=request.getAttribute("USER_ID")+"";
        String stid=param.get("stid");
        if(stid!=null&&!"".equals(stid)){
        	SysSheet sheet=new SysSheet();
        	sheet.setStid(Integer.parseInt(stid));
        	sheet.setStatus("1");
        	sheet.setFkOrderUersBasicinfoUid(Integer.parseInt(user_id));
        	sheetMapper.updateByPrimaryKeySelective(sheet);
        	
        	sheet=sheetMapper.selectByPrimaryKey(Integer.parseInt(stid));
        	UserBasicInfo info=infoMapper.selectByPrimaryKey(sheet.getFkSendUersBasicinfoUid());
        	result.put("mobilephone", info.getMobilephone());
        }
        Gson gson = new Gson();
        try {
            result.put("result",resultStr);
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));
        } catch (Exception e) {
            log.info("程序出错："+e);
        }
    }
}
